package patientAdministration;

import entity.MedicalCase;
import entity.Patient;
import java.sql.Timestamp;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import medicalDocumentation.medicalDoc.MedicalCaseSessionBean;

@Stateless
public class PatientSessionBean implements PatientSessionBeanRemote {

    @PersistenceContext
    private EntityManager entityManager;

    public PatientSessionBean() {
    }

    @Override
    public List<Patient> getAllPatients() {
        String strQuery = "SELECT p FROM Patient p WHERE p.isActive = 'true'";
        Query query = entityManager.createQuery(strQuery);
        return query.getResultList();
    }

    @Override
    public void deletePatient(Long pin) {
        Patient patient = entityManager.find(Patient.class, pin);
        entityManager.remove(patient);
        entityManager.flush();
    }

    @Override
    public void editPatient(Long pin, String firstName, String lastName, String gender, Timestamp birthday,
            String identification, String citizenship, String insurance, String paymentType,
            String address, String phone, String email) {
        Patient patient = entityManager.find(Patient.class, pin);
        patient.setFirstName(firstName);
        patient.setLastName(lastName);
        patient.setGender(gender);
        patient.setBirthday(birthday);
        patient.setIdentification(identification);
        patient.setCitizenship(citizenship);
        patient.setInsuranceNo(insurance);
        patient.setPaymentType(paymentType);
        patient.setPhone(phone);
        patient.setAddress(address);
        patient.setEmail(email);

        entityManager.flush();
    }

    @Override
    public List<Patient> getPatients(String firstName, String lastName, String birthday, String identificationNumber) {
        String strQuery = "SELECT p FROM Patient p WHERE";
        String and = " AND";

        boolean hasParameter = false;
        if (birthday != null) {
            strQuery = strQuery + " p.birthday = :inBirthday";
            hasParameter = true;
        } else if (firstName != null) {
            if (hasParameter) {
                strQuery = strQuery + and;
            }
            strQuery = strQuery + " p.firstName = :inFirstName";
            hasParameter = true;
        } else if (lastName != null) {
            if (hasParameter) {
                strQuery = strQuery + and;
            }
            strQuery = strQuery + " p.lastName = :inLastName";
            hasParameter = true;
        } else if (identificationNumber != null) {
            if (hasParameter) {
                strQuery = strQuery + and;
            }
            strQuery = strQuery + " p.idendificationNumber = :inIndentificationNubmer";
            hasParameter = true;
        }
        if (!hasParameter) {
            return null;
        }
        Query query = entityManager.createQuery(strQuery);

        if (birthday != null) {
            query.setParameter("inBithday", birthday);
        } else if (firstName != null) {
            query.setParameter("inFirstName", firstName);
        } else if (lastName != null) {
            query.setParameter("inLastName", lastName);
        } else if (lastName != null) {
            query.setParameter("inIndentificationNubmer", identificationNumber);
        }
        return query.getResultList();
    }

    @Override
    public List<Patient> getPatientsByIdentification(String citizenship, String id) {
        String strQuery = "SELECT p FROM Patient p WHERE p.citizenship = :citizenship AND p.identification = :id";
        Query query = entityManager.createQuery(strQuery);
        query.setParameter("citizenship", citizenship);
        query.setParameter("id", id);
        return query.getResultList();
    }

    @Override
    public long addNewPatient(String firstName, String lastName, String gender, Timestamp birthday,
            String identification, String citizenship, String insurance, String paymentType,
            String address, String phone, String email) {
        Patient patient = new Patient();
        patient.setFirstName(firstName);
        patient.setLastName(lastName);
        patient.setGender(gender);
        patient.setBirthday(birthday);
        patient.setIdentification(identification);
        patient.setCitizenship(citizenship);
        patient.setInsuranceNo(insurance);
        patient.setPaymentType(paymentType);
        patient.setPhone(phone);
        patient.setAddress(address);
        patient.setEmail(email);
        patient.setIsActive(true);
        entityManager.persist(patient);
        entityManager.flush();
        long patinetId = patient.getPin();
        return patinetId;
    }

    @Override
    public Patient getPatientByPIN(Long pin) {
        Patient patient = entityManager.find(Patient.class, pin);
        return patient;
    }

    @Override
    public Long addCase(Long pin, String date, String diagnosis, String doctor) {

        MedicalCase medCase = new MedicalCase();
        medCase.setCreateDate(date);

        medCase.setDiagnosis(diagnosis);
        medCase.setResponsibleDr(doctor);
        
        Patient patient = entityManager.find(Patient.class, pin);
        List<MedicalCase> clist = patient.getCases();
        clist.add(medCase);
        patient.setCases(clist);
        entityManager.persist(medCase);
        Long cin = medCase.getCIN();
        System.out.println("New case create: CIN= " + cin);
        entityManager.persist(patient);
        entityManager.flush();
        return cin;
    }
    
    @Override
    public void deleteCase(Long id) {
        MedicalCase mc = entityManager.find(MedicalCase.class, id);
        entityManager.remove(mc);
        entityManager.flush();
    }
    
}
